package com.zidian.leader.common.activity;

import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.zidian.leader.R;
import com.zidian.leader.activity.MainActivity;

import butterknife.ButterKnife;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by GongCheng on 2016/10/24 0024.
 */

public abstract class BaseActivity extends AppCompatActivity {

    protected Toolbar toolbar;
    protected Subscription subscription;
    protected CompositeSubscription compositeSubscription;
    protected ProgressDialog progressDialog;
    protected ActionBar actionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //禁用横屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(getLayout());
//        ActivityInfo activityInfo = null;
//        try {
//            activityInfo = getPackageManager().getActivityInfo(
//                    new ComponentName(getPackageName(), this.getClass().getCanonicalName()), PackageManager.GET_META_DATA);
//
//            if ((activityInfo.flags & ActivityInfo.FLAG_NO_HISTORY) == 0) {
//                // 拥有“noHistory”标志的 Activity 不放入“堆栈”中
//               // MyApplication.getInstance().addActivity(this);
//            }
//        } catch (PackageManager.NameNotFoundException e) {
//            e.printStackTrace();
//        }

        ButterKnife.bind(this);
        toolbar = ButterKnife.findById(this, R.id.toolbar);
        initProgressDialog();
        if (toolbar != null) {
            setSupportActionBar(toolbar);
            actionBar = getSupportActionBar();
            if (actionBar != null) {
                actionBar.setDisplayHomeAsUpEnabled(true);
            }
            if (!(this instanceof MainActivity)){
                toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onBackPressed();
                    }
                });
            }
        }
    }

    /**
     * 初始化progressDialog
     */
    private void initProgressDialog() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage(getString(R.string.loading));
    }

    protected void addSubscription(Subscription subscription) {
        if (compositeSubscription == null) {
            compositeSubscription = new CompositeSubscription();
        }
        compositeSubscription.add(subscription);
    }

    /**
     * 退出界面解除ButterKnife RxJava绑定
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        if (subscription != null && subscription.isUnsubscribed()) {
            subscription.unsubscribe();
        }
        if (compositeSubscription != null) {
            compositeSubscription.unsubscribe();
        }
    }

    /**
     * 设置布局ID
     * @return 布局ID
     */
    protected abstract int getLayout();

    protected void startActivity(Class<?> activity) {
        startActivity(new Intent(this,activity ));
    }
}
